package com.api.shopapi.common;

import java.util.ArrayList;
import java.util.List;

import com.huawei.push.javasdk.exception.HuaweiMesssagingException;
import com.huawei.push.javasdk.message.AndroidConfig;
import com.huawei.push.javasdk.message.AndroidNotification;
import com.huawei.push.javasdk.message.BadgeNotification;
import com.huawei.push.javasdk.message.ClickAction;
import com.huawei.push.javasdk.message.Message;
import com.huawei.push.javasdk.message.Notification;
import com.huawei.push.javasdk.messaging.HuaweiApp;
import com.huawei.push.javasdk.messaging.HuaweiCredential;
import com.huawei.push.javasdk.messaging.HuaweiMessaging;
import com.huawei.push.javasdk.messaging.HuaweiOption;
import com.huawei.push.javasdk.messaging.SendResponce;

public class HCMSamples {


    public HuaweiApp initializeApp() {
        // Create HuaweiCredential
        // This appId and appSecret come from Huawei Developer Alliance
        String appId = "101298757";
        String appSecret = "ff4c55080934277d79e97afd065e81b69c311ef8493f4c545e852bbdc1d8530c";
        HuaweiCredential credential = HuaweiCredential.builder()
                .setAppId(appId)
                .setAppSecret(appSecret)
                .build();

        // Create HuaweiOption
        HuaweiOption option = HuaweiOption.builder()
                .setCredential(credential)
                .build();

        // Initialize HuaweiApp
        return HuaweiApp.initializeApp(option);
    }

    public void sendToToken() throws HuaweiMesssagingException {
        // [START send_to_token]
        // Obtain initialized app
        HuaweiApp app = initializeApp();

        String token2 = getAccessToken(app,true);
        System.out.println(token2);

        String token = "YOUR_TOKEN";
        // Define a message payload.
        Message message = Message.builder()
                .setData("the message to the client app")
                .addToken(token2)
                .build();

        // Send a message to the device corresponding to the provided token
        SendResponce response = HuaweiMessaging.getInstance(app).send(message);
        // [END send_to_token]
    }

    public void sendToTopic() throws HuaweiMesssagingException {
        // [START send_to_topic]
        // Obtain initialized app
        HuaweiApp app = initializeApp();


        String topic = "YOUR_TOPIC";
        // Define a message payload.
        Message message = Message.builder()
                .setData("the message to the client app")
                .setTopic(topic)
                .build();

        // Send a message to the device corresponding to the provided topic
        SendResponce response = HuaweiMessaging.getInstance(app).send(message);
        // [END send_to_topic]
    }

    public void sendToCondition() throws HuaweiMesssagingException {
        // [START send_to_condition]
        // Obtain initialized app
        HuaweiApp app = initializeApp();

        String condition = "YOUR_CONDITION";
        // Define a message payload.
        Message message = Message.builder()
                .setData("the message to the client app")
                .setTopic(condition)
                .build();

        // Send a message to the device corresponding to the provided topic
        SendResponce response = HuaweiMessaging.getInstance(app).send(message);
        // [END send_to_condition]
    }

    public void sendValidateOnly() throws HuaweiMesssagingException {
        Message message = Message.builder()
                .setData("the message to the client app")
                .addToken("token")
                .build();

        HuaweiApp app = initializeApp();

        // [START send_validate_only]
        // Send a message not to the terminal
        boolean validateOnly = true;
        SendResponce response = HuaweiMessaging.getInstance(app).send(message, validateOnly);
        // [END send_validate_only]
    }

    public Message normalNotificationMessage() {
        String token = "YOUR_TOKEN";
        Notification notification = new Notification("Big News", "This is a Big News!");
        ClickAction clickAction = ClickAction.builder()
                .setType(1)
                .setIntent("#Intent;compo=com.rvr/.Activity;S.W=U;end")
                .build();
        BadgeNotification badgeNotification = new BadgeNotification(99, "Classic");
        AndroidNotification androidNotification = AndroidNotification.builder()
                .setTitle("Noti in Noti title")
                .setBody("Noti in Noti body")
                .setColor("#AACCDD")
                .setSound("/raw/shake")
                .setClickAction(clickAction)
                .setTitleLocKey("demo_title_new2")
                .addTitleLocArgs("title_args")
                .addTitleLocArgs("title_args2")
                .setBodyLocKey("demo_content_new2")
                .addBodyLocArgs("content_args")
                .addBodyLocArgs("content_args2")
                .setChannelId("RingRing")
                .setStyle(2)
                .setBigTitle("Big Boom Title")
                .setBigBody("Big BooBig Boom BodBig Boom BodBig Boom BodBig Boom BodBig Boom BodBig Boom BodBig Boom BodBig Boom BodBig Boom BodBig Boom Bodm Body")
                .setBigPicture("https://developer-portalres-drcn.dbankcdn.com/system/modules/org.opencms.portal.template.core/resources/images/icon_Promotion.png")
                .setAutoClear(86400000)
                .setNotifyId(486)
                .setBadge(badgeNotification)
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder()
                .setCollapseKey(-1)
                .setTtl("1448s")
                .setBiTag("Trump")
                .setFastAppTargetType(1)
                .setNotification(androidNotification)
                .build();

        String data = "k=v,k=v";
        Message message = Message.builder()
                .setNotification(notification)
                .setAndroidConfig(androidConfig)
                .addToken(token)
                .build();

        return message;
    }

    public Message normalTransparentMessage() {
        String token = "YOUR_TOKEN";

        HuaweiApp app = initializeApp();
        String token2 = getAccessToken(app,true);
        System.out.println(token2);

        Message message = Message.builder()
                .setData("the message to the client app")
                .addToken(token2)
                .build();
        return message;
    }

    public String getAccessToken(HuaweiApp app, boolean forceRefresh) {
        HuaweiCredential credential = app.getOption().getCredential();
        try {
            if (forceRefresh) {
                credential.refreshToken();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return credential.getAccessToken();
    }



}
